<?php

/**
 * documentation actions.
 *
 * @package    espace14
 * @subpackage documentation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class documentationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$results_per_page = 3;
    $first_page=1;  
    $this->pagination = new sfDoctrinePager('documentation', $results_per_page);  
    $this->pagination->setQuery(Doctrine_Core::getTable('documentation')->createQuery('a'));  
    $this->pagination->setPage($request->getParameter('page', $first_page));  
    $this->pagination->init();  
	$this->documentations = $this->pagination->getResults();
	$this->getResponse()->setTitle('Espace 14 | Documentation');
    // $this->documentations = Doctrine_Core::getTable('documentation')
      // ->createQuery('a')
      // ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->documentation = Doctrine_Core::getTable('documentation')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->documentation);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new documentationForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new documentationForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($documentation = Doctrine_Core::getTable('documentation')->find(array($request->getParameter('id'))), sprintf('Object documentation does not exist (%s).', $request->getParameter('id')));
    $this->form = new documentationForm($documentation);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($documentation = Doctrine_Core::getTable('documentation')->find(array($request->getParameter('id'))), sprintf('Object documentation does not exist (%s).', $request->getParameter('id')));
    $this->form = new documentationForm($documentation);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($documentation = Doctrine_Core::getTable('documentation')->find(array($request->getParameter('id'))), sprintf('Object documentation does not exist (%s).', $request->getParameter('id')));
    $documentation->delete();

    $this->redirect('documentation/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
		$file = $form->getValue('url');
		if($file)
		{
			$filename = $file->getOriginalName();
			$extension = $file->getExtension($file->getOriginalExtension());
			$file->save(sfConfig::get('sf_root_dir').sfConfig::get('sf_web_dir_name').sfConfig::get('sf_upload_dir_name').'/'.$filename);
			
			$page = $form->save();
			$page->setUrl($filename);
			$form= $page->save();
		}
		else
		{
			$page = $form->save();
			$id = $page->getId();
			$q = Doctrine_Query::create()->select()->from('documentation i')->where('i.id = ?',$id);
			$url = $q->fetchOne();
			$url = $url->getUrl();
			if ($url)
			{
				$page->setUrl($url);
				$form = $page->save();
			}
			else
			{
				$form = $page->save();
			}
		}
		 $this->redirect('documentation/index');
    }
	
  }
}
